LIBRARY ieee;
USE ieee.std_logic_1164.all;
USE ieee.std_logic_arith.all;
USE ieee.std_logic_unsigned.all;

ENTITY GameOver IS
	PORT(
		Clk : IN std_logic;
		ClkEleRom : IN std_logic;
		Start : IN std_logic;
		Finish : OUT std_logic;
		Reset : IN std_logic;
		
		--GameMode : IN std_logic_vector(1 downto 0);
		GameResult : IN std_logic_vector(1 DOWNTO 0);
		
		PrimAddr    : OUT std_logic_vector(9 downto 0);
		PrimWriteData    : OUT  std_logic_vector(15 downto 0);
		PrimWriteEnable  : OUT std_logic;

		RawP1Key : IN std_logic_vector(5 downto 0)
	);
END GameOver;

ARCHITECTURE rtl OF GameOver IS
	COMPONENT GameOverRom IS
	PORT
	(
		address		: IN STD_LOGIC_VECTOR (11 DOWNTO 0);
		clock		: IN STD_LOGIC ;
		q		: OUT STD_LOGIC_VECTOR (0 DOWNTO 0)
	);
	END COMPONENT;
	
	TYPE State_Type IS (Idle, ShowInfo, ShowInfo_Pre, ShowInfo_Read_W,
						ShowInfo_Read, ShowInfo_Write, ShowInfo_DoWrite,
						ShowInfo_Wait, Readkey, Ready);
	
	SIGNAL state : State_Type;
	SIGNAL sCounter : std_logic_vector(3 downto 0);
	
	SIGNAL RomAddr : std_logic_vector(11 DOWNTO 0);
	SIGNAL RomData : std_logic;

	SIGNAL PosX, PosY : std_logic_vector(4 downto 0);
BEGIN
--	cGameOverRam : GameOverRom PORT MAP(
--		address => RomAddr,
--		clock	=> ClkEleRom,
--		q(0)	=> RomData );

	PROCESS(Clk, Reset)
	BEGIN
		IF Reset = '0' THEN
			state <= Idle;
			Finish <= '0';
		ELSIF rising_edge(Clk) THEN
			CASE state IS
				WHEN Idle =>
					IF Start = '1' THEN
						state <= ShowInfo;
						PosX <= "00000";
						PosY <= "00001";
					ELSE
						state <= Idle;
					END IF;
					Finish <= '0';
				WHEN ShowInfo =>
					PrimWriteEnable <= '0';
					IF PosX = 25 THEN
						IF PosY = 18 THEN
							state <= ReadKey;
						ELSE
							PosX <= "00001";
							PosY <= PosY +1;
							state <= ShowInfo_Read_W;
						END IF;
					ELSE
						PosX <= PosX +1;
						state <= ShowInfo_Read_W;
					END IF;
				WHEN ShowInfo_Read_W =>
					state <= ShowInfo_Pre;
				WHEN ShowInfo_Pre =>
					RomAddr <= (GameResult+1) & PosY & PosX;
					sCounter <= (OTHERS => '0');
					state <= ShowInfo_Read;
				WHEN ShowInfo_Read =>
					IF sCounter = 2 THEN
						state <= ShowInfo_Write;
					ELSE
						sCounter <= sCounter +1;
						state <= ShowInfo_Read;
					END IF;
				WHEN ShowInfo_Write =>
					IF RomData = '1' THEN
						PrimWriteData <= "0000000000001001";
					ELSE
						PrimWriteData <= "0000000000000001";
					END IF;
					PrimAddr <= PosY & PosX;
					state <= ShowInfo_DoWrite;
				WHEN ShowInfo_DoWrite =>
				
					PrimWriteEnable <= '1';
					sCounter <= (OTHERS => '0');
					state <= ShowInfo_Wait;
				WHEN ShowInfo_Wait =>
					IF sCounter = 4 THEN
						state <= ShowInfo;
						PrimWriteEnable <= '0';
					ELSE
						sCounter <= sCounter +1;
					END IF;
				WHEN ReadKey =>
					IF RawP1Key(5) = '1' THEN
						state <= Ready;
					ELSE
						state <= ReadKey;
					END IF;
				WHEN Ready =>
					state <= Idle;
					Finish <= '1';
				WHEN OTHERS =>
					NULL;
			END CASE;															
		END IF;
	END PROCESS;
END rtl;
